/* n的阶乘 = 1*2*3...*n */
function jiecheng(n) {
    let ji = 1
    for (let i = 1; i <= n; i++) {
        ji *= i
    }
    return ji
}

function cachedFn(fn) {
    const cacheObj = {}

    return function cfn(...args) {

        let cachedResult = cacheObj[args]

        if (!cachedResult) {
            console.log("又开始运算了");
            const ret = fn.apply(this, args)
            cacheObj[args] = ret
            console.log("cacheObj=", cacheObj);
            return ret
        }

        console.log("轻松使用缓存");
        return cachedResult
    }
}

// for (let i = 1; i <= 1000; i++) {
//     console.log(
//         jiecheng(i)
//     );
// }
const cJiecheng = cachedFn(jiecheng)
console.log(
    cJiecheng(50)
);

console.log(
    cJiecheng(100)
);

console.log(
    cJiecheng(50)
);

console.log(
    cJiecheng(100)
);

// 释放闭包
// cJiecheng = null